#include <stdio.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#define MAX 1000

using namespace std;

float data[100][2];

int main(int argc, char** argv)
{
	if(argc <=1 )
	{
		printf("\nFormat command line:\nNormalize 2D Data.exe \"filename\" MAX_VALUE\n\n");
		return 0;
	}	
	char fileName[256];
	memset(fileName, 0, 256);
	strcpy(fileName, argv[1]);
	
	puts(fileName);
	
	
	float maxValue = 0;
	if(argc >= 2) 
		maxValue = atof(argv[2]);
	if(maxValue >= 10000 || maxValue <= 50)
	{
		maxValue = MAX;
		printf("\nSet max equal the default max value (1000)\n\n");
	}
	fstream file;
	float x;
	float min = 72637462, max = -734785384;
	file.open(fileName, ios::app | ios::in);
	if(!file.is_open())
		printf("Can't open file");
	for(int i = 0; i < 200; i++)
	{
		file >> x;
		if(x > max)
			max = x;
		if(x < min)
			min = x;
		data[i/2][i%2] = x;
	}
	 
	
	for(int i =0; i < 100; i++)
	{
		data[i][0] += fabs(min);
		data[i][1] += fabs(min);
	}
	max += fabs(min);
	float nor = maxValue / max;
	
	
	file.close();
	
	file.open("[Normalized]2Ddata.txt", ios::out);
	if(!file.is_open())
		printf("Can't open file");
	for(int i =0; i < 100; i++)
		file << (int)(data[i][0]*nor) << "\t"  << (int)(data[i][1]*nor) <<  endl;
	file.close();
}